package com.goblin.model.media.repository;

import com.goblin.common.response.PageHelper;
import com.goblin.common.response.Paged;
import com.goblin.model.base.BaseRepositoryImpl;
import com.goblin.model.media.po.Message;
import com.goblin.model.media.po.QMessage;
import com.querydsl.core.QueryResults;
import com.querydsl.jpa.JPQLQuery;

import javax.persistence.EntityManager;

public class MessageRepositoryImpl extends BaseRepositoryImpl<Message, Integer>
        implements MessageRepositoryCustom {

    public MessageRepositoryImpl(EntityManager entityManager) {
        super(Message.class, entityManager);
    }

    @Override
    public Paged<Message> paged(Integer chatId, PageHelper pageable) {
        JPQLQuery<Message> query = this.jpaQueryFactory.selectFrom(QMessage.message)
                .where(QMessage.message.deleteTime.isNull()).where(QMessage.message.chat.id.eq(chatId))
                .orderBy(QMessage.message.date.desc());
        query = (JPQLQuery<Message>) getQuerydsl(pageable, query);
        QueryResults<Message> results = query.fetchResults();
        return new Paged<Message>(results.getTotal(), results.getResults());
    }
}
